<%@ page import="mybatis.pojo.Student" %>
<%@ page import="mybatis.services.StudentService" %>
<%@ page import="mybatis.util.StrUtil" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="java.io.File" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<%--
  Created by IntelliJ IDEA.
  User: Dell
  Date: 2022/7/4
  Time: 14:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>


<%
    DiskFileItemFactory d = new DiskFileItemFactory();
    ServletFileUpload sp = new ServletFileUpload(d);
    sp.setHeaderEncoding("utf-8");


    List<FileItem> fileItems = sp.parseRequest(request);
    Student student = new Student();
    //Map<String ,String> map=new HashMap<>();
    for (FileItem f : fileItems) {
        out.print("<p>");
        if (f.isFormField()) {
            out.print("表单：名字是" + f.getFieldName() + "值是" + f.getString("utf-8"));
            switch (f.getFieldName()) {
                case "sname":
                    student.setSname(f.getString("utf-8"));

                    break;
                case "gender":
                    student.setSex(f.getString("utf-8"));
                    break;
                case "tel":
                    student.setTelephone(f.getString("utf-8"));


                    break;
                case "qq":
                    student.setQq(f.getString("utf-8"));

                    break;
                case "wechat":
                    student.setWechat(f.getString("utf-8"));


                    break;
                case "gradeID":
                    student.setGradeid(StrUtil.toInt(f.getString("utf-8")));
                    break;
                case "enterDate":
                    student.setEnterdate(StrUtil.toDate(f.getString("utf-8")));
                    break;
                case "city":
                    student.setFromcity(f.getString("utf-8"));
                    break;
                case "birth":
                    student.setBirthday(StrUtil.toDate(f.getString("utf-8")));

                    break;
                case "address":
                    student.setAddress(f.getString("utf-8"));


                    break;
                case "psd":
                    student.setPass(f.getString("utf-8"));
                    break;
                case "idcardpic":
                    student.setIdcardpic(f.getString("utf-8"));
                    break;

            }


            // map.put(f.getFieldName(),f.getString("utf-8"));
//        }else{
//            out.print("这是一个文件域，名字是"+f.getName());
//            if(f.getSize()<1)
//                continue;
//            out.print("文件名是："+f.getName());
//            String fileName=f.getName();
//            String houzhui=fileName.substring(fileName.lastIndexOf('.'));
//            String newName= UUID.randomUUID()+houzhui;
//            File newPic=new File("d://images");
//            if(newPic.exists()==false)
//                newPic.mkdirs();
//            d.setRepository(newPic);
//            f.write(new File(newPic,newName));
//            map.put(f.getFieldName(),"images/"+newName);
//        }


        } else {
            out.print("这是一个文件域，名字是" + f.getFieldName());
            if (f.getSize() < 1)
                continue;
            out.print("文件名是：" + f.getName());
            String fileName = f.getName();
            String houzhui = fileName.substring(fileName.lastIndexOf('.'));
            String newName = UUID.randomUUID() + houzhui;
            File newPic = new File(application.getRealPath("images"));
            if (newPic.exists() == false)
                newPic.mkdirs();

            f.write(new File(newPic, newName));
            student.setIdcardpic("images/" + newName);///!!!!!!!
        }
    }
        out.print("</p>");

        int add = StudentService.ME.add(student);
        if (add > 0) {
            out.print("增加成功");
        } else {
            out.print("增加失败");
        }
//        if (add > 0) {
//            //表单重复提交：1）后退再提交 2）直接刷新
//            //重定向，可以解决刷新一下，提交两次的问题
////        response.sendRedirect("student.jsp");
//            // request.getRequestDispatcher("subject.jsp").forward(request,response);
//        } else {
//            // out.print("增加失败，可能课程名重复了！");
//            request.setAttribute("msg", "增加失败，可能学生电话名重复了！");
//            request.getRequestDispatcher("studentAdd.jsp").forward(request, response);
//            //response.sendRedirect("subjectAdd.jsp");
//        }
//    Set<Map.Entry<String, String>> entries = map.entrySet();
//    Iterator<Map.Entry<String, String>> iterator = entries.iterator();
//    while (iterator.hasNext()){
//        out.print(iterator.next()+"<br/>");
//    }

//    student.setSname(map.get("sname"));
//    student.setSex(map.get("gender"));
//    student.setTelephone(map.get("tel"));
//    student.setQq(map.get("qq"));
//    student.setWechat(map.get("wechat"));
//    student.setGradeid(StrUtil.toInt(map.get("gradeID"),1));
//    student.setFromcity(map.get("city"));
//    out.print(map.get("city"));
//    student.setEnterdate(StrUtil.toDate(map.get("enterDate")));
//    student.setBirthday(StrUtil.toDate(map.get("birth")));
//    student.setAddress(map.get("address"));
//    student.setPass(map.get("upass"));
//    student.setIdcardpic(map.get("idcardpic"));
//    out.print(map.get("idcardpic"));
%>


</body>
</html>

